package com.tykj.yangningyu.chatdemo;

import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.avos.avoscloud.im.v2.AVIMClient;
import com.avos.avoscloud.im.v2.AVIMConversation;
import com.avos.avoscloud.im.v2.AVIMException;
import com.avos.avoscloud.im.v2.AVIMMessageManager;
import com.avos.avoscloud.im.v2.AVIMTypedMessage;
import com.avos.avoscloud.im.v2.AVIMTypedMessageHandler;
import com.avos.avoscloud.im.v2.callback.AVIMConversationCallback;
import com.avos.avoscloud.im.v2.messages.AVIMTextMessage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.avos.avoscloud.im.v2.AVIMMessageManager.registerMessageHandler;

public class ChatActivity extends BaseActivity {


    @BindView(R.id.msg_list)
    ListView msgList;
    @BindView(R.id.et_msg)
    EditText etMsg;
    @BindView(R.id.send)
    Button send;
    private AVIMConversation conversation;
    private List<String> mData=new ArrayList<>();
    private MsgAdapter adapter;

    @Override
    protected void initView() {

        Intent intent = getIntent();
        String conv = intent.getStringExtra("conv");
        conversation = ChatApp.mClient.getConversation(conv);
        Log.i("exception", "initView: =======+++++++++==="+conv);
        adapter = new MsgAdapter();
        msgList.setAdapter(adapter);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_chat;
    }

    @Override
    protected void onResume() {
        super.onResume();
        //这里指定只处理AVIMTextMessage类型的消息
        registerMessageHandler(AVIMTextMessage.class,new CustomMessageHandler<>());
    }

    @Override
    protected void onPause() {
        super.onPause();
        //这里指定只处理AVIMTextMessage类型的消息
        AVIMMessageManager.unregisterMessageHandler(AVIMTextMessage.class,new CustomMessageHandler<>());
    }

    @OnClick(R.id.send)
    public void onClick() {
        final String msg = etMsg.getText().toString().trim();
        mData.add(msg);
        adapter.setData(mData);
        Log.i("data", "onClick: ++++++++++++"+mData.size());
        AVIMTextMessage avmsg = new AVIMTextMessage();
        avmsg.setText(msg);
        conversation.sendMessage(avmsg, new AVIMConversationCallback() {
            @Override
            public void done(AVIMException e) {

                Toast.makeText(ChatActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
     class CustomMessageHandler<AVIMTextMessage> extends AVIMTypedMessageHandler {
         @Override
         public void onMessage(AVIMTypedMessage message, AVIMConversation conversation, AVIMClient client) {
//             super.onMessage(message, conversation, client);
             if(message != null){
                 String from = message.getFrom();
                 Log.i("from", "onMessage: "+from);
                 String content = message.getContent();
                 mData.add(content);
                 adapter.setData(mData);
             }
         }
     }
}
